package gdma.hqdes.stat.bean;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import gdma.hqdes.bean.Unit;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wangyue
 * @Version 3.0
 * @Date 2024-08-28
 */
@Data
public class StatQuery {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);

    @TableId
    Integer id;

    @ApiModelProperty("年份/开始年份")
    Integer year;

    @ApiModelProperty("截止年份")
    Integer year1;

    @ApiModelProperty("单位级别")
    Integer scope;

    @ApiModelProperty("单位列表：unitId外，其中地市级列表包含可能Z、Y这样区域编号")
    String unitComboJson;

    @ApiModelProperty("统计项列表")
    String indexComboJson;

    @ApiModelProperty("创建时间")
    Date createdTime;

    @JsonIgnore
    @TableField(exist = false)
    List<Unit> units;

    @TableField(exist = false)
    List<String> unitCombos;

    @TableField(exist = false)
    List<IndexCombo> indexCombos;

    @JsonIgnore
    @SneakyThrows
    public List<String> getUnitCombos() {
        if(unitCombos != null) return unitCombos;
        if(StringUtils.hasText(unitComboJson)) unitCombos = OBJECT_MAPPER.readValue(unitComboJson, new TypeReference<List<String>>() {});
        return unitCombos;
    }

    @JsonIgnore
    public String getUnitCombo() {
        return CollectionUtils.isEmpty(getUnitCombos()) ? null : unitCombos.get(0);
    }

    @SneakyThrows
    public void setUnitCombos(List<String> unitCombos) {
        this.unitCombos = unitCombos;
        unitComboJson = OBJECT_MAPPER.writeValueAsString(unitCombos);
    }

    public void setUnitCombos(String... combos) {
        setUnitCombos(Arrays.asList(combos));
    }

    public Collection<Long> getUnitIds() {
        return getUnitCombos().stream().map(combo -> {
            try {
                return Long.valueOf(combo);
            } catch(Exception x) {
                return null;
            }
        }).filter(v -> v != null).collect(Collectors.toList());
    }

    public Long getUnitId() {
        try {
            String combo = getUnitCombos().get(0);
            return Long.valueOf(combo);
        } catch(Exception x) {
            return null;
        }
    }

    public void setUnitId(Long parentUnitId) {
        setUnitCombos(Arrays.asList(String.valueOf(parentUnitId)));
    }

    @JsonIgnore
    @SneakyThrows
    public List<IndexCombo> getIndexCombos() {
        if(indexCombos != null) return indexCombos;
        if(StringUtils.hasText(indexComboJson)) indexCombos = OBJECT_MAPPER.readValue(indexComboJson, new TypeReference<List<IndexCombo>>() {});
        return indexCombos;
    }

    @JsonIgnore
    public IndexCombo getIndexCombo() {
        return CollectionUtils.isEmpty(getIndexCombos()) ? null : indexCombos.get(0);
    }

    @SneakyThrows
    public void setIndexCombos(List<IndexCombo> indexCombos) {
        this.indexCombos = indexCombos;
        indexComboJson = OBJECT_MAPPER.writeValueAsString(indexCombos);
    }

    public void setIndexCombo(IndexCombo... combos) {
        setIndexCombos(Arrays.asList(combos));
    }

}
